package com.sea.operate.controller;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;

import java.io.*;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TestController {
    public static void main(String[] args) {
        // 下载链接列表
        List<String> fileUrls = new ArrayList<>();
        fileUrls.add("http://cozylux.oss-cn-beijing.aliyuncs.com/d7e0ad0b-e3a4-4f67-9cef-16c0d634bb86.xlsx?Expires=3307680893&OSSAccessKeyId=LTAI4G55LJEk8nARtrjYngou&Signature=%2FSQmBUslkzhY80CuIR3sAqdFwYE%3D");
        fileUrls.add("http://cozylux.oss-cn-beijing.aliyuncs.com/05f71a12-0fad-4ac3-832c-690682fca0c2.xlsx?Expires=3307680867&OSSAccessKeyId=LTAI4G55LJEk8nARtrjYngou&Signature=ZAxzYRbsnZnF3dkHarlO5%2FRuxS0%3D");

        // 压缩包输出路径
        try {
            ByteArrayInputStream zipInputStream = createZipStreamFromUrls(fileUrls);
            System.out.println("ZIP 文件流已生成，可用于进一步处理！");
            // 例如将流写入文件或传递给 HTTP 响应


            OSSClient ossClient=new OSSClient("http://oss-cn-beijing.aliyuncs.com",
                    "LTAI4G55LJEk8nARtrjYngou",
                    "XPbdMs0xrRGAsmKVm9VYC8bSTTuFhl");
            InputStream inputStream = zipInputStream;
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("application/octet-stream");
            String filename = "PO1085.zip";
            //获取文件后缀名
            String prefix=filename.substring(filename.lastIndexOf(".")+1);
            // //修改后的文件名
            String fileUUIDname = UUID.randomUUID().toString();
            //修改后的文件名(带后缀)
            String NewFileName = fileUUIDname + "." + prefix;
            ossClient.putObject("cozylux", NewFileName, inputStream,metadata);
            // 关闭OSSClient。
            ossClient.shutdown();
            Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 50);
            String url = ossClient.generatePresignedUrl("cozylux", NewFileName, expiration).toString();
            System.out.println(url);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static ByteArrayInputStream createZipStreamFromUrls(List<String> fileUrls) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try (ZipOutputStream zipOut = new ZipOutputStream(byteArrayOutputStream)) {
            for (String fileUrl : fileUrls) {
                addUrlFileToZip(fileUrl, zipOut);
            }
        }

        // 将 ByteArrayOutputStream 转为 ByteArrayInputStream 供调用方读取
        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    }

    private static void addUrlFileToZip(String fileUrl, ZipOutputStream zipOut) {
        try (InputStream in = new URL(fileUrl).openStream()) {
            String fileName = Paths.get(new URL(fileUrl).getPath()).getFileName().toString();
            ZipEntry zipEntry = new ZipEntry(fileName);
            zipOut.putNextEntry(zipEntry);

            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) > 0) {
                zipOut.write(buffer, 0, len);
            }
            zipOut.closeEntry();
            System.out.println("已添加文件到压缩包：" + fileName);

        } catch (IOException e) {
            System.err.println("下载失败：" + fileUrl);
            e.printStackTrace();
        }
    }

}
